package com.amir.common.weight

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.viewpager.widget.ViewPager
import java.util.jar.Attributes


class AutoHeightViewPager constructor(context: Context, attributes: AttributeSet?) :
    ViewPager(context, attributes) {


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var height = 0;
        for (i in 0 until getChildCount()) {
            var child = getChildAt(i);
            if (child.visibility != GONE) {
                child.measure(
                    widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
                )
                var h = child.getMeasuredHeight();
                if (h > height) {
                    height = h;
                }
            }
        }
        var newHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, newHeight);

    }

}